package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishByCategoryIdVO;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController("adminDishController")
@RequestMapping("/admin/dish")
@Api(tags = "菜品控制器")
@Slf4j
public class DishController {
    @Resource
    DishService dishService;

    /**
     * 查(分页查)
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> findAll(DishPageQueryDTO dishPageQueryDTO){
        log.info("[分页查询] 开始执行");
        PageResult pageResult = dishService.findAll(dishPageQueryDTO);
        log.info("[分页查询] 查询完毕");
        return Result.success(pageResult);
    }

    /**
     * 查(id查)
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查")
    public Result<DishVO> findById(@PathVariable Long id){
        log.info("[根据id查询菜品] 开始执行");
        DishVO dishVO = dishService.findById(id);
        if(dishVO==null){
            log.info("[根据id查询菜品] 查询失败");
            return Result.error("菜品查询失败");
        }else{
            log.info("[根据id查询菜品] 查询成功");
            return Result.success(dishVO);
        }
    }

    /**
     * 查(categoryId查)
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据categoryId查")
    public Result<List<Dish>> findByCategoryId(Long categoryId){
        log.info("[根据categoryId查] 开始执行");
        List<Dish> dishList = dishService.findByCategoryId(categoryId);
        if(dishList!=null){
            log.info("[根据categoryId查] 查询成功");
            return Result.success(dishList);
        }else{
            log.info("[根据categoryId查] 查询失败");
            return Result.error("根据categoryId查询失败");
        }
    }


    /**
     * 改
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result<String> modify(@RequestBody DishDTO dishDTO){
        log.info("[修改菜品] 开始执行");
        Integer tag = dishService.modify(dishDTO);
        if(tag>0){
            log.info("[修改菜品] 修改成功");
            return Result.success();
        }else{
            log.info("[修改菜品] 修改失败");
            return Result.error("菜品修改失败");
        }
    }

    /**
     * 增
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result<String> add(@RequestBody DishDTO dishDTO){
        log.info("[新增菜品] 开始执行");
        Integer tag = dishService.add(dishDTO);
        if(tag>0){
            log.info("[新增菜品] 执行成功");
            return Result.success();
        }else{
            log.info("[新增菜品] 新增失败");
            return Result.error("新增菜品失败");
        }
    }

    /**
     * 删
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result<String> remove(Integer[] ids){
        log.info("[批量删除菜品] 开始执行");
        Integer tag= dishService.remove(ids);
        if(tag>0){
            log.info("[批量删除菜品] 删除成功");
            return Result.success();
        }else{
            log.info("[批量删除菜品] 删除失败");
            return Result.error("删除菜品失败");
        }
    }

    /**
     * 菜品起售/停售
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售/停售")
    public Result<String> setStatus(Long id,@PathVariable Integer status){
        log.info("[菜品起售/停售] 开始执行");
        Integer tag = dishService.setStatus(id,status);
        if(tag>0){
            log.info("[菜品起售/停售] 执行完毕");
            return Result.success();
        }else{
            log.info("[菜品起售/停售] 执行失败");
            return Result.error("菜品状态更改失败");
        }
    }


}